package com.ef.web.action;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;

import com.ef.dao.ContractDao;
import com.ef.domain.User;
import com.ef.web.domain.CRPerformance;

public class CRPerformanceAction extends ReportAction {

	private static final long serialVersionUID = 1L;

	private ContractDao contractDao;

	private String month;
	private String year;

	private List<CRPerformance> result;

	public String list() {

		if (year == null) {
			SimpleDateFormat formater = new SimpleDateFormat("yyyy");
			year = formater.format(new Date());
		}
		if (month == null) {
			SimpleDateFormat formater = new SimpleDateFormat("MM");
			month = formater.format(new Date());
		}
		User user = getSessionUser();
		result = contractDao.performanceByCR(year + "-" + month, user.getSchool().getId());

		return LIST;
	}
	
	public void xml() {

		if (year == null) {
			SimpleDateFormat formater = new SimpleDateFormat("yyyy");
			year = formater.format(new Date());
		}
		if (month == null) {
			SimpleDateFormat formater = new SimpleDateFormat("MM");
			month = formater.format(new Date());
		}
		User user = getSessionUser();
		result = contractDao.performanceByCR(year + "-" + month,  user.getSchool().getId());

		Element chart = DocumentFactory.getInstance().createElement("chart");
		Element series = DocumentFactory.getInstance().createElement("series");
		Element graphs = DocumentFactory.getInstance().createElement("graphs");
		Element graph1 = DocumentFactory.getInstance().createElement("graph");
		chart.add(series);
		chart.add(graphs);
		graph1.addAttribute("gid", "1");
		Element graph2 = DocumentFactory.getInstance().createElement("graph");
		graph2.addAttribute("gid", "2");

		graphs.add(graph1);
		graphs.add(graph2);

		
		int xId = 0;
		for(CRPerformance rc:result) {
			Element value = DocumentFactory.getInstance().createElement("value");
			value.addAttribute("xid", String.valueOf(xId));
			value.addText(rc.getTitle());
			Element g1Value = DocumentFactory.getInstance().createElement("value");
			g1Value.addAttribute("xid", String.valueOf(xId));
			g1Value.addText(String.valueOf(rc.getCall()));
			Element g2Value = DocumentFactory.getInstance().createElement("value");
			g2Value.addAttribute("xid", String.valueOf(xId++));
			g2Value.addText(String.valueOf(rc.getShowup()));

			series.add(value);
			graph1.add(g1Value);
			graph2.add(g2Value);
		}
		
		try {
			HttpServletResponse httpResponse = ServletActionContext.getResponse();
			httpResponse.setContentType("text/xml");
			httpResponse.setCharacterEncoding("UTF-8");
			PrintWriter out = httpResponse.getWriter();
			out.write(chart.asXML());
		} catch (Exception ignore) {
			ignore.printStackTrace();
		}
		return;
	}

	public String export() {
		list();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("doc.filename", " CR Performance");
		map.put("doc.title", "CR");
		map.put("doc.line", "CR Performance (  " + year + "-" + month + " )");
		map.put("doc.data", result);
		exportExcel3(getResponse(), map);

		return NONE;
	}

	public ContractDao getContractDao() {
		return contractDao;
	}

	public void setContractDao(ContractDao contractDao) {
		this.contractDao = contractDao;
	}

	public List<CRPerformance> getResult() {
		return result;
	}

	public void setResult(List<CRPerformance> result) {
		this.result = result;
	}

	public String getMonth() {
		return month;
	}

	public void setMonth(String month) {
		this.month = month;
	}

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}

}
